package com.yjt.class_studio.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjt.class_studio.entity.CmsStudio;
import com.yjt.class_studio.entity.SysUser;
import com.yjt.class_studio.pojo.dto.CmsStudioSearchDTO;
import com.yjt.class_studio.service.CmsStudioService;
import com.yjt.class_studio.service.SysUserService;
import com.yjt.class_studio.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Yjt
 * @Date 2023/6/19 18:29
 * @Description
 */
@RestController
@RequestMapping("/studio")
@Api(tags = "工作室管理")
public class CmsStudioController {
    @Autowired
    private CmsStudioService cmsStudioService;

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "分页查询工作室")
    @PostMapping("/getStudioList")
    public R getStudioList(@RequestBody CmsStudioSearchDTO cmsStudioSearchDTO){
        Page<CmsStudio> page = new Page<>(cmsStudioSearchDTO.getPageNum(),cmsStudioSearchDTO.getPageSize());
        IPage<CmsStudio> studio = cmsStudioService.page(page, new QueryWrapper<CmsStudio>().like("studio_name", cmsStudioSearchDTO.getStudioName()));
        studio.getRecords().forEach(item->{
            SysUser teacher = sysUserService.getById(item.getStudioTeacherId());
            item.setStudioTeacherName(teacher.getNickname());
        });
        return R.ok().put("studioList",studio);
    }

    @DeleteMapping("/deleteStudioById")
    @ApiOperation(value = "删除工作室")
    public R deleteStudio(Integer id){
        boolean b = cmsStudioService.removeById(id);
        if(b)
            return R.ok("删除成功");
        else
            return R.error("删除失败");
    }

    @PostMapping("/updateStudio")
    @ApiOperation(value = "更新工作室")
    public R updateStudio(@RequestBody CmsStudio cmsStudio){
        CmsStudio studio = cmsStudioService.getById(cmsStudio.getId());
        if(studio != null){
            cmsStudio.setUpdateTime(new Date());
            boolean b = cmsStudioService.updateById(cmsStudio);
            if(b)
                return R.ok("更新成功");
            else
                return R.error("更新失败");
        }else{
            return R.error("找不到此条数据");
        }
    }

    @PostMapping("/saveStudio")
    @ApiOperation(value = "新增工作室")
    public R saveStudio(@RequestBody CmsStudio cmsStudio){
        cmsStudio.setCreateTime(new Date());
        boolean b = cmsStudioService.save(cmsStudio);
        if(b)
            return R.ok("新增成功");
        else
            return R.error("新增失败");
    }

    @GetMapping("/getStudioById")
    @ApiOperation(value = "根据id查找工作室")
    public R getStudioById(@RequestParam Integer id){
        CmsStudio studio = cmsStudioService.getById(id);
        if(studio != null){
            return R.ok().put("studio",studio);
        }else{
            return R.error("找不到此条数据");
        }
    }
}
